/*
 * copyright 2010 - LDP - Desenvolvimento Profissional e Eventos S/S
 * programacao: Carlos Andre Begale Martins
 * e-mail: carlinbegale@hotmail.com
 */

import java.text.SimpleDateFormat
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
import java.util.Locale

class FormatarTagLib {

    /**
     * formata moeda do brasil <g:moeda valor=" ${ valor } " />
     */
    def moeda = { attrs ->
        if (!attrs.containsKey('valor')) 
        throwTagError("Esta faltando o atributo valor na Tag moeda.")
        def val = attrs.get('valor') 
        if (val == null) 
        return
        def format = attrs.get('format')
        if (!format) { 
            format = "#,##0.00"
        }
        DecimalFormat formatar = new DecimalFormat(format, new DecimalFormatSymbols(new Locale("pt","BR")))
        out << formatar.format((Double)val) 
    }
	
    /**
     * formata milhar  <g:moeda valor=" ${ valor } " />
     */
    def milhar = { attrs ->
        if (!attrs.containsKey('valor')) 
        throwTagError("Esta faltando o atributo valor na Tag moeda.")
        def val = attrs.get('valor') 
        if (val == null) 
        return
        def format = attrs.get('format')
        if (!format) { 
            format = "#,##0"
        }
        DecimalFormat formatar = new DecimalFormat(format, new DecimalFormatSymbols(new Locale("pt","BR")))
        out << formatar.format((Double)val) 
    }
	
    /**
     * formata porcentagem <g:porcentagem valor="${ valor } " />
     */
    def porcentagem = { attrs ->
        if (!attrs.containsKey('valor')) 
        throwTagError("Esta faltando o atributo valor na Tag porcentagem.")
        def val = attrs.get('valor') 
        if (val == null) 
        return
        def format = attrs.get('format')
        if (!format) { 
            format = "0.##"
        }
        DecimalFormat formatar = new DecimalFormat(format, new DecimalFormatSymbols(new Locale("pt","BR")))
        out << formatar.format((Double)val) 
    }
	
    // chama o loading para procedimentos demorados (precisa chamar a funcao load() no onsubmit do formulario - onsubmit="load();")
    def loading = { attrs ->
        def val = attrs.get('imagem')
        if (!val)
            val = "../images/loading.gif"
        def msg = attrs.get('mensagem')
        if (!msg)
            msg = "Processando. Aguarde!"
        def jq = attrs.get('jquery')
        if(!jq)
            jq = true
        def ret = """<div id="loading" align="center" style="text-align:center;
                                                      font: bold 13px Arial, Helvetica, sans-serif;
                                                      margin:20px 0;">  
				<font color="black">${msg}</font><br>
				<img src="${resource(dir:'images',file:'loading.gif')}" alt="${msg}" align="center" />  
			</div>"""
        if(jq == true)
            ret = ret + """<script language="Javascript" src="${resource(dir:'js',file:'jquery.min.js')}"></script>"""
        ret = ret + """<script language="Javascript" src="${resource(dir:'js',file:'loading.js')}"></script>"""
        out << ret 
    }
}
